<template>
  <div class="addCarType">

    <!-- Form -->
    <el-dialog width="600px" :visible="true" :before-close="cancelBtn" :close-on-click-modal="false">
      <div slot="title">
        <div class="addTitle">{{ title }}</div>
      </div>
      <el-form :model="form" :label-width="formLabelWidth">

        <el-form-item label="banner图片:">
          <image-upload v-model="form.image" :limit="1"/>
        </el-form-item>
        <el-form-item label="banner标题:" >
          <el-input v-model="form.title" autocomplete="off" placeholder="输入banner标题" style="width: 80%"></el-input>
        </el-form-item>
        <el-form-item label="banner位置:">
          <el-select v-model="form.location" placeholder="请选择" style="width: 80%">
            <el-option :label="item.label" :value="item.value" v-for="(item,index) in locationList"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="跳转地址:" >
          <el-input v-model="form.redirect_to.short_link" autocomplete="off" placeholder="输入跳转地址" style="width: 80%"></el-input>
        </el-form-item>
        <el-form-item label="跳转参数:" >
          <el-input v-model="form.redirect_to.extra_data" autocomplete="off" placeholder="输入跳转参数" style="width: 80%"></el-input>
        </el-form-item>
        <el-form-item label="排序:" >
          <el-input v-model="form.sequence" type="number" style="width: 80%" min="1" @change="()=>{if(form.bannerSort<1){form.bannerSort=1}}" autocomplete="off" placeholder="输入banner排序"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelBtn">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {bannerAdd, bannerEdit} from "@/api"
export default {
  name: "addCarType",
  props:['item','state','closeAlert'],
  data(){
    return{
      form: {
        image:'',
        title:'',
        redirect_type:2,
        redirect_to:{
          short_link:'',
          extra_data:'',
        },
        sequence:'',
        location:''
      },
      formLabelWidth: '120px',
      advertisData: [],
      locationList: [
        {
          label:'首页',
          value:1
        },{
          label:'心理电台',
          value:2
        },{
          label:'心理测评',
          value:3
        },{
          label:'数字附能',
          value:4
        },
      ],
      title:'新增banner'
    }
  },
  mounted() {

    if(this.state == 'edit'){
      this.info()
      this.title="修改banner"
    }else{
      this.title="新增banner"
    }
  },
  methods:{
    // 详情
    async info(){
      this.form = this.item
    },
    // 确认
    async confirm(){
      if(this.state == 'add'){
        // 添加
        this.handleConvenientSave();
      }else{
        // 修改
        this.handleConvenientEdit()
      }

    },
   async handleConvenientSave(){
      await bannerAdd(this.form).then(res=>{
        if (res.code==10000){
              this.$message({
              message: res.message,
              type: 'success'
            });
            this.cancelBtn();
        }
      })
    },
   async handleConvenientEdit(){
      await bannerEdit(this.form.id,this.form).then(res=>{
        if (res.code==10000){
          this.$message({
            message: res.message,
            type: 'success'
          });
          this.cancelBtn();
        }
      })
    },
    // 关闭弹窗
    cancelBtn(){
      this.$emit('closeAlert',false)
    }
  }
}
</script>

<style scoped lang="less">
.addTitle{
  font-size: 28px;
  font-family: PingFangSC-Semibold, PingFang SC;
  font-weight: 600;
  color: #000000;
}
/deep/.el-dialog__header,
/deep/.el-dialog__body,
/deep/.el-dialog__footer{
  padding-left: 45px;
  padding-right: 45px;
}
/deep/.el-form-item__label{
  font-size: 14px;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: #000000;
}
///deep/.el-form-item__content input{
//  border: none;
//  background: #F4F4F4;
//  border-radius: 10px;
//}
.el-select{
  width: 100%;
}
</style>
